package com.hnjfzn.message.sms.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.hnjfzn.common.core.utils.http.HttpUtils;
import com.hnjfzn.message.sms.config.HuLianConfig;
import com.hnjfzn.message.sms.domain.bo.SmsParams;
import com.hnjfzn.message.sms.domain.bo.UtilResult;
import com.hnjfzn.message.sms.service.IThirdService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 互联365短信
 */
@Service
public class ThirdHuLianServiceImpl implements IThirdService {

    private final static String HOST = "https://zwp.market.alicloudapi.com";

    @Override
    public UtilResult send(SmsParams params) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobile", params.getPhone());
        map.put("content", params.message());

        UtilResult ur;
        try {
            // 发送短信
            String result = HttpUtils.sendGet(HOST.concat("/sms/sendv2"), map, Collections.singletonMap("Authorization", "APPCODE " + HuLianConfig.getAppCode()));
            // 结果解析
            JSONObject jsonObject = JSONObject.parseObject(result);
            // 数据
            ur = UtilResult.builder().success(jsonObject.getInteger("error_code").equals(0)).msg(jsonObject.getString("reason")).result(result).build();
        } catch (Exception e) {
            ur = UtilResult.builder().success(false).msg("获取验证码异常").build();
        }
        return ur;
    }
}
